package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputCmbPanel;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Employee;

public class PanelEmpUI extends JPanel implements ActionListener, FocusListener {
	private static final long serialVersionUID = 1L;
	
	private InputPanel pEmpNo;
	private InputPanel pEmpName;
	private InputCmbPanel pTitle;
	private InputCmbPanel pManager;
	private InputPanel pSalary;
	private InputCmbPanel pDeptNo;
	
	private JPanel panel_Bottom;
	private JPanel panel_Center;
	private JButton btnAdd;
	private String btnTxt;
	private HashMap<String, Integer> hashDepts;
	private HashMap<String, Integer> hashTitles;
	private HashMap<String, Integer> hashMgn;
	private EmployeeDAO dao;
	
	public PanelEmpUI(String btnTxt, EmployeeDAO dao) {
		super();
		this.btnTxt=btnTxt;
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		setPreferredSize(new Dimension(320, 320));
		panel_Center = new JPanel(new GridLayout(0, 1, 0, 0));
		add(panel_Center);
		
		pEmpNo = new InputPanel("번호");
		pEmpNo.getTf().addFocusListener(this);
		panel_Center.add(pEmpNo);
		
		pEmpName = new InputPanel("이름");
		panel_Center.add(pEmpName);
		
		pTitle = new InputCmbPanel("직책");
		try {
			hashTitles = dao.selectedTitleList();
			Set<String> arr= hashTitles.keySet();
			String[] titleItems = new String[hashTitles.size()];
			arr.toArray(titleItems);
			pTitle.addItems(titleItems);
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		panel_Center.add(pTitle);
		
		pDeptNo = new InputCmbPanel("부서");
		try {
			hashDepts = dao.selectedDeptList();
			Set<String> arr= hashDepts.keySet();
			String[] deptItems = new String[hashDepts.size()];
			arr.toArray(deptItems);
			pDeptNo.addItems(deptItems);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		/*pDeptNo.getCmb().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int selectDeptno = hashDepts.get(pDeptNo.getSelectItem());
				pManager.getCmb().removeAllItems();
				try {
					hashMgn =dao.selectedManager();
					Set<String> arr= hashMgn.keySet();
					String[] mgnItems = new String[hashMgn.size()];
					arr.toArray(mgnItems);
					
					pManager.addItems(mgnItems);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});*/
		
		panel_Center.add(pDeptNo);
		
		pManager = new InputCmbPanel("매니저");
		try {
			hashMgn =dao.selectedManager();
			Set<String> arr= hashMgn.keySet();
			String[] mgnItems = new String[hashMgn.size()];
			arr.toArray(mgnItems);
			pManager.addItems(mgnItems);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		panel_Center.add(pManager);
		
		pSalary = new InputPanel("급여");
		panel_Center.add(pSalary);
		
		panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		if (!btnTxt.equals("추가")){
			panelDisable();
		}
		
		btnAdd = new JButton(btnTxt);
		if (btnTxt.equals("검색")){
			panelDisable();
		}
		
		btnAdd.addActionListener(this);
		panel_Bottom.add(btnAdd);
	}

	private Employee getDept(){
		return new Employee(
				Integer.parseInt(pEmpNo.getTf().getText().trim()), 
				pEmpName.getTf().getText(), 
				hashTitles.get(pTitle.getSelectItem()==null?"":pTitle.getSelectItem()), 
				hashMgn.get(pManager.getSelectItem()==null?"":pManager.getSelectItem()), 
				Integer.parseInt(pSalary.getTf().getText().trim()), 
				hashDepts.get(pDeptNo.getSelectItem()==null?"":pDeptNo.getSelectItem())
				);
	}
	
	private void panelDisable() {
		pEmpNo.getTf().setEnabled(true);
		pEmpName.getTf().setEnabled(false);
		pTitle.getCmb().setEnabled(false);
		pManager.getCmb().setEnabled(false);
		pSalary.getTf().setEnabled(false);
		pDeptNo.getCmb().setEnabled(false);
	}
	
	private void panelEnable(){
		pEmpNo.getTf().setEnabled(false);
		pEmpName.getTf().setEnabled(true);
		pTitle.getCmb().setEnabled(true);
		pManager.getCmb().setEnabled(true);
		pSalary.getTf().setEnabled(true);
		pDeptNo.getCmb().setEnabled(true);
	}
	
	private void panelClear(){
		pEmpNo.setTfTxt("");
		pEmpName.setTfTxt("");
		pTitle.setDefaultSelectIndex(0);
		pManager.setDefaultSelectIndex(0);
		pSalary.setTfTxt("");
		pDeptNo.setDefaultSelectIndex(0);
	}
	
	@Override
	public void focusGained(FocusEvent e) {}

	@Override
	public void focusLost(FocusEvent e) {
		searchItem();
		if (btnAdd.getText().equals("수정")){
			panelEnable();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가"))
			actionPerformedBtnAdd(e);
		else if (e.getActionCommand().equals("수정")) 
			actionPerformedBtnUpdate(e);
		else if (e.getActionCommand().equals("검색"))
			actionPerformedBtnSearch(e);
		else if (e.getActionCommand().equals("삭제"))
			actionPerformedBtnDel(e);
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {
		Employee d = getDept();
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this,"입력되었습니다");
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"입력실패 다시 확인하세요.");
		}
	}
	
	private void actionPerformedBtnDel(ActionEvent e) {
		Employee d = getDept();
		try {
			dao.deleteItemByNo(d);
			JOptionPane.showMessageDialog(this,"삭제되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,e1);
		}
		
	}

	private void actionPerformedBtnUpdate(ActionEvent e) {
		Employee d = getDept();
		try {
			dao.updateItemByNo(d);
			JOptionPane.showMessageDialog(this,"수정되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"수정실패 다시 확인하세요.");
		}		
	}

	private void actionPerformedBtnSearch(ActionEvent e) {
		searchItem();
	}

	private void searchItem(){
		int no = Integer.parseInt(pEmpNo.getTf().getText().trim());
		Employee d = new Employee();
		d.setEmpNo(no);
		try {
			d = dao.searchItem(d);
			pEmpName.setTfTxt(d.getEmpName());
			pTitle.setSelectItem(dao.selectedTitleById(d.getTitle()));
			pManager.setSelectItem(dao.selectedNameByEmpNo(d.getManager()));
			pSalary.setTfTxt(d.getSalary()+"");
			pDeptNo.setSelectItem(dao.selectedDeptNameByDeptno(d.getDeptno()));
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (NullPointerException e1){
			if (btnAdd.getText().equals("추가")){
				JOptionPane.showMessageDialog(this, "추가 가능");
			}else{
				JOptionPane.showMessageDialog(this, "존재하지 않음");
				pEmpNo.setTfTxt("");
			}
			
		}
	}
}
